package com.example.mediamoudle.multivideo

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Message
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.baselibrary.base.BaseActivity
import com.android.baselibrary.utils.getDurationString
import com.example.mediamoudle.R
import com.example.mediamoudle.crop.VideoCropActivity
import com.example.mediamoudle.databinding.ActivityMultiVideoBinding
import com.example.mediamoudle.router.MediaRouterPath

@Route(path = MediaRouterPath.MEDIA_MULTI_VIDEO)
class MultiVideoActivity : BaseActivity<ActivityMultiVideoBinding>(),View.OnClickListener {
    private var videoList = listOf<String>(
        "/storage/emulated/0/444.mp4",
       /* "/storage/emulated/0/125.mp4",
        "/storage/emulated/0/70651.mp4"*/
    )
    override fun getLayoutId(): Int  = R.layout.activity_multi_video

    override fun initView() {
          mBinding.videoPreview.setVideoPath(videoList)
          mBinding.videoPreview.start()
    }

    override fun initListener() {
        mBinding.imagePlay.setOnClickListener(this)
        mBinding.imagePlayControl.setOnClickListener(this)
        mBinding.videoPreview.setOnClickListener {
            if (mBinding.videoPreview.isPlaying){
                mBinding.imagePlay.visibility = View.VISIBLE
                mBinding.videoPreview.pause()
                mBinding.imagePlayControl.isSelected = true
            }
        }
    }
    override fun onClick(v: View?) {
        when(v?.id){
            /**点击播放**/
            R.id.image_play ->{
                mBinding.imagePlay.visibility = View.GONE
                mBinding.videoPreview.start()
                mBinding.imagePlayControl.isSelected = false
                startHandler()
                //videoBottomDialog?.show()
            }
            /**暂停and播放按钮**/
            R.id.image_play_control ->{
                mBinding.imagePlayControl.isSelected = mBinding.videoPreview.isPlaying
                mBinding.imagePlay.visibility = if (mBinding.videoPreview.isPlaying) View.VISIBLE else View.GONE
                if (mBinding.videoPreview.isPlaying){
                    mBinding.videoPreview.pause()
                }else{
                    mBinding.videoPreview.start()
                    startHandler()
                }
            }
        }
    }
    /**
     * * @Description 发送消息更新进度条
     * @Author yf
     * @Date 2022/11/15 13:50
     */
    private fun startHandler(){
        if (mBinding.videoPreview.isPlaying){
            handler.sendEmptyMessageDelayed(VideoCropActivity.MESSAGE,50)
        }

    }
    /**
     * * @Description 更新进度条
     * @Author yf
     * @Date 2022/11/15 13:50
     */
    private val handler: Handler = @SuppressLint("HandlerLeak")
    object : Handler(){
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when(msg.what){
                VideoCropActivity.MESSAGE ->{
                    val current =  mBinding.videoPreview.curPosition
                    //   Log.i("TAGas", "initPic1: ${px2dp(this@VideoCropActivity,(mBinding.mRecyclerView.layoutManager as LinearLayoutManager).getChildAt(0)?.measuredWidth!!)}")

                    mBinding.tvBeginDuration.text = getDurationString((current).toLong())
                    mBinding.progressBar.secondaryProgress = (current/1000)
                    mBinding.progressBar.progress =  (current/1000)
                    startHandler()
                }
            }
        }
    }

}